package com.sky.task;

import com.sky.mapper.admin.ReportMapper;
import com.sky.pojo.UserReport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 自定义定时任务，实现计算当日新增用户和全部用户的定时处理
 */
@Component
@Slf4j
public class ReportTask {
    @Autowired
    private ReportMapper reportMapper;

    @Scheduled(cron = "0 0 2 * * ?")
    public void adduserStatistics(){
        LocalDate now = LocalDate.now();

        String date = now.minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        Integer newUser = reportMapper.userStatistics(now.minusDays(1),now);
        Integer totalUser = reportMapper.userStatistics(null,now);
        UserReport userReport = UserReport.builder()
                .date(date)
                .newUser(newUser)
                .totalUser(totalUser)
                .build();
        reportMapper.addUserReport(userReport);
    }
}
